package com.hzjq.encrypt.pop

import android.content.Context
import android.text.TextUtils
import com.hzjq.encrypt.R
import com.hzjq.encrypt.bean.Lg
import com.hzjq.encrypt.util.EToast
import com.hzjq.encrypt.util.KeyBoradX
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import kotlinx.android.synthetic.main.pop_lg_editor_layout.view.*

class LgEditorPop : CenterPopupView {

    private lateinit var data: Lg
    private lateinit var c:OnLgEditorCallback

    constructor(context: Context) : super(context)

    override fun getImplLayoutId(): Int {
        return R.layout.pop_lg_editor_layout
    }

    override fun onCreate() {
        if (data == null) {
            data = Lg()
        }
        lgPopUidEt.setText(data.uid)
        lgPopYxqEt.setText(data.yxq)
        lgPopGzmEt.setText(data.gzm)
        lgPopFbhEt.setText(data.fbh)
        lgPopCwxxEt.setText(data.gzmcwxx)

        lgPopCancelText.setOnClickListener {
            hideKeyBord()
            dismiss()
        }

        lgPopSureText.setOnClickListener {
            if (TextUtils.isEmpty(lgPopUidEt.text)) {
                EToast.toast("请输入雷管uid")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(lgPopYxqEt.text)) {
                EToast.toast("请输入雷管有效期")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(lgPopGzmEt.text)) {
                EToast.toast("请输入雷管工作码")
                return@setOnClickListener
            }
            data.uid = lgPopUidEt.text.toString()
            data.yxq = lgPopYxqEt.text.toString()
            data.gzm = lgPopGzmEt.text.toString()
            data.fbh = lgPopFbhEt.text.toString()
            data.gzmcwxx = lgPopCwxxEt.text.toString()
            hideKeyBord()
            dismiss()
            c.onLgEditor(data)
        }
    }

    private fun hideKeyBord(){
        KeyBoradX.hideKeyBoard(lgPopUidEt)
        KeyBoradX.hideKeyBoard(lgPopYxqEt)
        KeyBoradX.hideKeyBoard(lgPopGzmEt)
        KeyBoradX.hideKeyBoard(lgPopFbhEt)
        KeyBoradX.hideKeyBoard(lgPopCwxxEt)
    }

    interface OnLgEditorCallback {
        fun onLgEditor(lg: Lg)
    }


    companion object {
        fun show(context: Context,c:OnLgEditorCallback,data: Lg?){
            val pop = LgEditorPop(context)
            if(data != null) {
                pop.data = data
            }
            pop.c = c
            XPopup.Builder(context)
                .asCustom(pop)
                .show()
        }
    }
}